package com.zmc.product.controller;


import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.product.infrastructure.entity.CommodityCategoryPropertyEntity;
import com.zmc.product.infrastructure.bean.query.CommodityCategoryPropertyQuery;
import com.zmc.product.infrastructure.entity.CommodityCategoryPropertyValueEntity;
import com.zmc.product.infrastructure.service.CommodityCategoryPropertyService;
import com.zmc.product.infrastructure.service.CommodityCategoryPropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.util.Arrays;
import java.util.List;

import static com.zmc.common.response.AjaxResult.success;


/**
 * 商品中心的属性Controller
 * @author zmc
 */
@RestController
@RequestMapping("/property")
public class CategoryPropertyController {
    @Autowired
    private CommodityCategoryPropertyService commodityCategoryPropertyService;

    @Autowired
    private AuthTokenService authTokenService;

    @Autowired
    private CommodityCategoryPropertyValueService commodityCategoryPropertyValueService;

    /**
     * 查询商品中心的属性列表
     */

    @PostMapping("/list")
    public R list(@RequestBody CommodityCategoryPropertyQuery query) {
        PageUtils page = commodityCategoryPropertyService.pageCommodityCategoryPropertyList(query);
        return R.page(page);
    }

    /**
     * 导出商品中心的属性列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityCategoryPropertyQuery query) {
        List<CommodityCategoryPropertyEntity> list = commodityCategoryPropertyService.selectCommodityCategoryPropertyList(query);
        ExcelUtil<CommodityCategoryPropertyEntity> util = new ExcelUtil<>(CommodityCategoryPropertyEntity.class);
        util.exportExcel(response, list, "商品中心的属性数据");
    }

    /**
     * 获取商品中心的属性详细信息
     */
    @PostMapping("/info")
    public R getInfo(@RequestBody CommodityCategoryPropertyEntity commodityCategoryProperty) {
        return R.ok(commodityCategoryPropertyService.getById(commodityCategoryProperty.getId()));
    }

    /**
     * 新增商品中心的属性
     */
    @PostMapping("/add")
    public R add(@RequestBody CommodityCategoryPropertyEntity commodityCategoryProperty) {
        String username = authTokenService.getUserInfo().getUsername();
        commodityCategoryProperty.setCreateBy(username);
        commodityCategoryPropertyService.save(commodityCategoryProperty);
        return R.ok();
    }

    /**
     * 修改商品中心的属性
     */
    @PostMapping("/update")
    public R edit(@RequestBody CommodityCategoryPropertyEntity commodityCategoryProperty) {
        String username = authTokenService.getUserInfo().getUsername();
        commodityCategoryProperty.setUpdateBy(username);
        commodityCategoryPropertyService.updateById(commodityCategoryProperty);
        return R.ok();
    }

    /**
     * 删除商品中心的属性
     */
    @PostMapping("/delete")
    public R remove(@RequestBody CommodityCategoryPropertyEntity commodityCategoryProperty) {
        Long count = commodityCategoryPropertyValueService.countByCategoryId(commodityCategoryProperty.getId());
        if (count > 0) {
            return R.error("请先删除属性值列表");
        }
        commodityCategoryPropertyService.removeById(commodityCategoryProperty.getId());
        return R.ok();
    }
}
